package org.jiamandan.com.strategy;

import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.strategy.enums.JSYEventTagEnum;
import org.jiamandan.com.strategy.impl.tram.IJSYDZBasicListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Jmd

 * @see:
 * @创建日期：2023-03-23 14:08
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-03-23 14:08	         Jmd        	创建
 * @end
 */
@Service
@Slf4j
public class JSYDZContextService {

    @Autowired
    private final Map<String, IJSYDZBasicListener> strategyMap = new ConcurrentHashMap<>();

    public JSYDZContextService(Map<String, IJSYDZBasicListener> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach((k, v) -> this.strategyMap.put(k, v));
    }

    public void execute(String cmd, String payload, ChannelHandlerContext ctx) throws Exception {
        String className = JSYEventTagEnum.getValueByKey(cmd);
        log.info("接收到嘉满电-JSY报文，CMD=" + cmd);
        if (StringUtils.isNotBlank(className)) {
            strategyMap.get(className).doExecute(payload, ctx);
        }
    }
}
